package com.xiaogao.serviceEdu.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaogao.serviceEdu.entity.EduTeacher;
import com.xiaogao.serviceEdu.entity.vo.TeacherQuery;
import com.xiaogao.serviceEdu.mapper.EduTeacherMapper;
import com.xiaogao.serviceEdu.service.EduTeacherService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author 小高
 * @since 2021-05-29
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Override
    public void pageQuery(Page<EduTeacher> pageParam, TeacherQuery teacherQuery) {
        // 构建条件
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");// 排序：升序

        if (teacherQuery == null){
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }

        // 多条件组分查询
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)) {
            queryWrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_create",end);
        }
        baseMapper.selectPage(pageParam, queryWrapper);
    }

    // 前台分页查询讲师功能
    @Override
    public Map<String, Object> getPageList(Page<EduTeacher> pageTeacher) {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        // 升序
        wrapper.orderByAsc("sort");
        // 把分页数据封装到pageTeacher中
        baseMapper.selectPage(pageTeacher,wrapper);
        // 获取分页数据封装到map中
        List<EduTeacher> records = pageTeacher.getRecords();
        long current = pageTeacher.getCurrent(); // 当然页
        long pages = pageTeacher.getPages(); // 当前分页总页数
        long size = pageTeacher.getSize(); // 每页条数大小（limit）
        long total = pageTeacher.getTotal(); // 总条数
        boolean hasNext = pageTeacher.hasNext(); // 是否存在下一页
        boolean hasPrevious = pageTeacher.hasPrevious(); // 是否存在上一页

        Map<String, Object> map = new HashMap<>();
        map.put("items",records);
        map.put("current",current);
        map.put("pages",pages);
        map.put("size",size);
        map.put("total",total);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);

        return map;
    }

    @Override
    public boolean removeById(Serializable id) {
        Integer result = baseMapper.deleteById(id);
        return null != result && result > 0;
    }
}
